
package ro.imc.rest.server.serviceimpl;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;

import javax.ws.rs.core.MediaType;

import org.apache.commons.collections.Transformer;

import ro.imc.rest.client.services.forserver.RestService;
import ro.imc.rest.server.transformer.RestCourseToLearningObjectTransformer;
import ro.imc.rest.server.util.Connection;
import ro.imc.rest.server.util.ListTransformer;
import ro.imc.rest.server.util.RestConfiguration;
import ro.imc.rest.shared.dto.LearningObject;
import ro.imc.rest.shared.dto.RestLogin;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.WebResource;

import de.imc.clixrestdto.course.RestCourse;
import de.imc.clixrestdto.course.RestCourseList;

/**
 * The server side implementation of the RPC service.
 */
@SuppressWarnings("serial")
public class RestServiceImpl extends RemoteServiceServlet
	implements RestService {
	public static final String COURSES_PATH = "/lms/courses";
	public static final String CATALOGS_PATH = "/lms/catalogs";
	public static final String SEARCHES_PATH = "/queries";
	
	@Override
	public RestLogin loginToRestApi(String user, String password) {
		//maybe other auth methods to CLIX server
		return new RestLogin(user, password);
	}

	@Override
	public List<LearningObject> getCoursesList(RestLogin user) {
		String restUrl = RestConfiguration.getSetting("resturl") + COURSES_PATH;
		List<LearningObject> learningObjectList = new ArrayList<LearningObject>();
		try {
			URI uri = new URI(restUrl);
//			Connection connection = new Connection(uri, user.getName(), user.getPassword());
//			WebResource web = connection.getWebResource();
//			RestCourseList coursesWrapper = web.accept(MediaType.APPLICATION_XML).get(RestCourseList.class);
//			Transformer transformer = new RestCourseToLearningObjectTransformer();
//			ListTransformer<LearningObject> listTransformer = new ListTransformer<LearningObject>();
//			learningObjectList = listTransformer.transformAll(coursesWrapper.getRestCourses(), transformer);
		} catch (URISyntaxException e) {
			System.err.println(" error on: " + restUrl);
		}
		return learningObjectList;
	}

	@Override
	public void startCourse(RestLogin user, String courseId) {
		String restUrl = RestConfiguration.getSetting("resturl") + COURSES_PATH;
		try {
			URI uri = new URI(restUrl);
//			Connection connection = new Connection(uri, user.getName(), user.getPassword());
//			WebResource web = connection.getWebResource();
//			RestCourseList coursesWrapper = web.accept(MediaType.APPLICATION_XML).get(RestCourseList.class);
//			courses = coursesWrapper.getRestCourses();
			
		} catch (URISyntaxException e) {
			System.err.println(" error on: " + restUrl);
		}
		
	}

	@Override
	public void finishCourse(RestLogin user, String courseId) {
		String restUrl = RestConfiguration.getSetting("resturl") + COURSES_PATH;
		try {
			URI uri = new URI(restUrl);
//			Connection connection = new Connection(uri, user.getName(), user.getPassword());
//			WebResource web = connection.getWebResource();
//			RestCourseList coursesWrapper = web.accept(MediaType.APPLICATION_XML).get(RestCourseList.class);
//			courses = coursesWrapper.getRestCourses();
			
		} catch (URISyntaxException e) {
			System.err.println(" error on: " + restUrl);
		}
		
	}

	@Override
	public void bookCourse(RestLogin user, String courseId) {
		String restUrl = RestConfiguration.getSetting("resturl") + COURSES_PATH;
		try {
			URI uri = new URI(restUrl);
//			Connection connection = new Connection(uri, user.getName(), user.getPassword());
//			WebResource web = connection.getWebResource();
//			RestCourseList coursesWrapper = web.accept(MediaType.APPLICATION_XML).get(RestCourseList.class);
//			courses = coursesWrapper.getRestCourses();
			
		} catch (URISyntaxException e) {
			System.err.println(" error on: " + restUrl);
		}
	}

	@Override
	public String getCoursesListResponse(RestLogin user) {

		return null;
	}

	@Override
	public void startCourseResponse(RestLogin user, String courseId) {

		
	}

	@Override
	public void finishCourseResponse(RestLogin user, String courseId) {

		
	}

	@Override
	public void bookCourseResponse(RestLogin user, String courseId) {

		
	}
	

}
